<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 头部操作栏 -->
            <div class="layui-form lay-header">
                <div class="toolbar">
                    <div class="layui-form-item lay-query-content">
                        <div class="lay-query-box">
                            <div class="query-lt">
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">签收任务编号：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="logisticsNo" class="layui-input" type="text" placeholder="请输入采购单编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">采购单编号：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="sn" class="layui-input" type="text" placeholder="请输入采购单编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">运输方式：</label>
                                    <div class="layui-input-inline mr0">
                                        <select id="queryTransportMode" name="transportMode" lay-filter="transportMode"></select>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">运单编号：</label>
                                    <div class="layui-input-inline mr0">
                                        <input name="waybillNo" class="layui-input" type="text" placeholder="请输入运单编号"/>
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label w-auto">创建时间：</label>
                                    <div class="layui-input-inline">
                                        <input name="createTime" id="createTime" class="layui-input date-icon" type="text" placeholder="请选择创建时间" style="width: 200px" readonly />
                                    </div>
                                    <input name="startCreateTime" id="sCreateTime" type="hidden" />
                                    <input name="endCreateTime" id="eCreateTime" type="hidden" />
                                </div>
                            </div>
                            <div class="query-rt">
                                <div class="layui-inline">
                                    <button class="layui-btn icon-btn color-green" lay-filter="formSubSearchLog" lay-submit>查询</button>
                                    <button class="layui-btn icon-btn color-white" lay-filter="resets" lay-submit>重置</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="lay-btn-rows">
                    <div class="lay-btn-box">
                        <div class="lay-btn-lt">
                            <button id="purchaseSignTaskBtnAdd" class="layui-btn icon-btn color-reseda">添加采购签收任务</button>
                        </div>
                        <div class="lay-btn-rt"></div>
                    </div>
                </div>
            </div>
            <!-- 表格 -->
            <table class="layui-table" id="purchaseSignTaskTable" lay-filter="purchaseSignTaskTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="purchaseSignTaskTableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="view">详情</a>
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    <!-- 条件判断：如果 status == '1' 才显示签收按钮 -->
    {{# if(d.status === '1') { }}
    <a class="layui-btn layui-btn-warm layui-btn-xs" lay-event="qianView">签收</a>
    {{# } }}
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="purchaseSignTaskForm">
    <form lay-filter="purchaseSignTaskForm" class="layui-form open-form">
        <div class="open-model">
            <div class="open-auto">
                <input name="id" type="hidden">
                <div class="layui-col-xs3">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;">供应商分类</label>
                        <div class="layui-input-block" style="margin-left: 130px;">
                            <div class="layui-input-inline">
                                <input id="supplierCategoryId" type="hidden" name="supplierCategoryId"/>
                                <div id="selectSupplierCategory" style="width: 240px"></div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs3">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;">供应商</label>
                        <div class="layui-input-block" style="margin-left: 130px;">
                            <select id="supplierId" name="supplierId" lay-filter="supplierId" lay-search></select>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs3">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;">采购订单</label>
                        <div class="layui-input-block" style="margin-left: 130px;">
                            <select id="purchaseOrderId" name="purchaseOrderId" lay-filter="purchaseOrderId" lay-search></select>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs3">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;">供应商联系人</label>
                        <div class="layui-input-block" style="margin-left: 130px;">
                            <input type="text" name="businessContact" class="layui-input"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs3">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;">采购部门</label>
                        <div class="layui-input-block" style="margin-left: 130px;">
                            <input type="text" name="departmentName" class="layui-input"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs3">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;">采购申请人</label>
                        <div class="layui-input-block" style="margin-left: 130px;">
                            <input type="text" name="creatorName" class="layui-input"/>
                        </div>
                    </div>
                </div>
                <!--        <div class="layui-col-xs3">-->
                <!--            <div class="layui-form-item">-->
                <!--                <label class="layui-form-label" style="width: 100px;">订单审核人</label>-->
                <!--                <div class="layui-input-block" style="margin-left: 130px;">-->
                <!--                    <input type="text" name="auditorNickname" class="layui-input"/>-->
                <!--                </div>-->
                <!--            </div>-->
                <!--        </div>-->
                <div class="layui-col-xs3">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;">运输方式</label>
                        <div class="layui-input-block" style="margin-left: 130px;">
                            <select id="transportMode" name="transportMode" lay-filter="transportMode"></select>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs3">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;">运单编号</label>
                        <div class="layui-input-block" style="margin-left: 130px;">
                            <input type="text" name="waybillNo" class="layui-input" lay-verify="required"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs3" id="plateNoDiv" style="display: none">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;">车牌号</label>
                        <div class="layui-input-block" style="margin-left: 130px;">
                            <input type="text" name="plateNo" class="layui-input"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs3" id="courierNoDiv" style="display: none">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;">快递单号</label>
                        <div class="layui-input-block" style="margin-left: 130px;">
                            <input type="text" name="courierNo" class="layui-input"/>
                        </div>
                    </div>
                </div>
                <div class="layui-col-xs3">
                    <div class="layui-form-item">
                        <label class="layui-form-label" style="width: 100px;">车辆联系电话</label>
                        <div class="layui-input-block" style="margin-left: 130px;">
                            <input type="text" name="phone" class="layui-input"/>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div>
                        <div class="layui-btn" id="addProduct" style="margin-top: 10px;">添加发货物料</div>
                        <div id="productList">
                            <table class="layui-table" id="purchaseSignTaskProductTable" lay-filter="purchaseSignTaskProductTable"></table>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item text-right">
                <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
                <button class="layui-btn" lay-filter="purchaseSignTaskFormSubmit" lay-submit>提交</button>
            </div>
        </div>
    </form>

</script>

<!--运输方式下拉框-->
<script type="text/html" id="transportModeTemplate">
    <form class="layui-form model-form" style="padding: 0;">
        <select name="transportMode" id="distCode" lay-filter="distCode" lay-verify="required"></select>
    </form>
</script>

<!--发货物料-->
<script type="text/html" id="purchaseSignTaskProductList">
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-body">
                <!-- 头部操作栏 -->
                <div class="layui-form lay-header">
                    <div class="toolbar">
                        <div class="layui-form-item lay-query-content">
                            <div class="lay-query-box">
                                <div class="query-lt">
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">物料编号：</label>
                                        <div class="layui-input-inline mr0">
                                            <input name="skuCode" class="layui-input" type="text" placeholder="请输入物料编号"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <label class="layui-form-label w-auto">物料名称：</label>
                                        <div class="layui-input-inline mr0">
                                            <input name="productName" class="layui-input" type="text" placeholder="请输入物料名称"/>
                                        </div>
                                    </div>
                                    <div class="layui-inline">
                                        <button class="layui-btn icon-btn color-green" lay-filter="deliveryRecordProductFormSubSearchLog" lay-submit>查询</button>
                                        <button class="layui-btn icon-btn color-white" lay-filter="deliveryRecordProductResets" lay-submit>重置</button>
                                        <button class="layui-btn layui-btn-normal icon-btn" id="confirmChoose">确认选择</button>
                                    </div>
                                </div>
                                <div class="query-rt">
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- 表格 -->
                <table class="layui-table" id="purchaseSignTaskProductListTable" lay-filter="purchaseSignTaskProductListTable"></table>
            </div>
        </div>
    </div>
</script>

<!--发货数量-->
<script type="text/html" id="deliveryQuantity">
    {{# var deliveryQuantity = "";
    if(d.deliveryQuantity != null){
    deliveryQuantity = d.deliveryQuantity;
    }
    }}
    <div><input name="quantity" placeholder="请输入发货数量" type="text" value="{{deliveryQuantity}}" lay-verify="required|quantity|compare" required class="layui-input" maxlength="10" /></div>
</script>

<!-- 表格操作列 -->
<script type="text/html" id="purchaseSignTaskProductTableBar">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<!-- 详情弹窗 -->
<script type="text/html" id="purchaseSignTaskView">
    <form lay-filter="purchaseSignTaskView" class="layui-form model-form">
        <input name="id" type="hidden"/>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">签收任务编号</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="logisticsNo" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">采购订单</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="sn" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">供应商</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="corporateName" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">供应商联系人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="businessContact" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">采购部门</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="departmentName" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">采购申请人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="purchaseApplicant" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">订单审核人</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="auditorName" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">运输方式</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="transportModeDesc" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">运单编号</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="waybillNo" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3" id="plateNoDetailDiv" style="display: none">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">车牌号</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="plateNo" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3" id="courierNoDetailDiv" style="display: none">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">快递单号</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="courierNo" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-col-xs3">
            <div class="layui-form-item">
                <label class="layui-form-label" style="width: 100px;">车辆联系电话</label>
                <div class="layui-input-block" style="margin-left: 130px;">
                    <input type="text" name="phone" class="layui-input"/>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <div style="margin-left: 30px;">
                <table class="layui-table" id="purchaseSignTaskDetailTable" lay-filter="purchaseSignTaskDetailTable"></table>
            </div>
        </div>
    </form>
</script>


<!-- js部分 -->
<script>
    layui.use(['layer', 'form', 'table', 'admin', 'laydate'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var config = layui.config;
        var admin = layui.admin;
        var upload = layui.upload;

        var laydate = layui.laydate;

        laydate.render({
            elem: "#createTime",
            type: "date",
            range: true,
            done: function (value, date, endDate) {
                if (value) {
                    var dateValue = value.split(" - ");
                    $("input[name='startCreateTime']").val(dateValue[0] + " 00:00:00");
                    $("input[name='endCreateTime']").val(dateValue[1] + " 23:59:59");
                } else {
                    $("input[name='startCreateTime']").val("");
                    $("input[name='endCreateTime']").val("");
                }
            },
        });

        //运输方式
        admin.getDictList("queryTransportMode","transport_mode", null, "运输方式");

        // 渲染表格
        var insTb = table.render({
            elem: '#purchaseSignTaskTable',
            url: config.base + 'enterprise/purchase/admin/purchaseDeliveryRecord/list',
            page: true,
            cellMinWidth: 100,
            cols: [[
                {type: 'numbers', title: '序号', width: 70}
                ,{field: 'logisticsNo', align: "center", title: '签收任务编号'}
                ,{field: 'sn', align: "center", title: '采购单编号'}
                ,{field: 'corporateName', align: "center", title: '供应商', width: 200}
                ,{field: 'skuNum', align: "center", title: '发货物料数'}
                ,{field: 'totalAmount', align: "center", title: '金额'}
                ,{field: 'waybillNo', align: "center", title: '运单编号'}
                ,{field: 'transportModeDesc', align: "center", title: '运输方式'}
                ,{field: 'statusName', align: "center", title: '状态'}
                ,{field: 'createTime', align: "center", title: '创建时间'}
                ,{align: 'center', toolbar: '#purchaseSignTaskTableBar', title: '操作', width: 300}
            ]]
        });

        // 工具条点击事件
        table.on('tool(purchaseSignTaskTable)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') { //修改
                showEditModel(data);
            } else if (obj.event === 'del') { // 删除
                doDelete(obj);
            } else if (obj.event === 'view') { // 详情
                showViewModel(data);
            } else if (obj.event === 'qianView') { // 签收
                openForm("add",data);
            }



        });

        // 搜索
        form.on('submit(formSubSearchLog)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}}, 'data');
        });

        // 重置搜索
        form.on('submit(resets)', function (data) {
            $(".lay-header").find('select').val("");
            $(".lay-header").find('input').val("");
            let field = data.field;
            for (let key in field) {
                field[key] = ''
            }
            insTb.reload({where: field, page: {curr: 1}}, 'data');
        });

        //监听排序
        table.on('sort(purchaseSignTaskTable)', function(obj){
            table.reload('purchaseSignTaskTable', {
                initSort: obj,
                where: {
                    sort: obj.field,
                    order: obj.type
                }
            });
        });

        // 添加按钮点击事件
        $('#purchaseSignTaskBtnAdd').click(function () {
            showEditModel();
        });

        function openForm(eventName, data) {
            admin.formOpen({
                area: ["90%", "85%"],
                offset: "50px",
                title: "添加签收记录",
                path: "components/purchase/purchaseSignForForm.html",
                success: function () {
                    parentData.eventName = eventName;
                    parentData.data = data;
                    parentData.operateTypes = "qianShou";
                }
            });
        }

        // 显示编辑弹窗2
        function showEditModel(data) {
            admin.open({
                type: 1,
                area: ['80%', '90%'],
                offset: ['8%', '15%'],
                title: data ? '修改采购签收任务' : '添加采购签收任务',
                content: $('#purchaseSignTaskForm').html(),
                success: function () {
                    //判断是否是修改，是修改则把选中的都给赋值进入表格中
                   if(data!=null){
                        admin.req('enterprise/purchase/admin/purchaseDeliveryRecordProduct/list', {limit: 99999, page: 1, purchaseDeliveryRecordId: data.id}, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                let dataS=res.data;
                                for (let i = 0; i < dataS.length; i++) {
                                    dataS[i].purchaseOrderDetailId =  dataS[i].id;
                                    deliveryRecordProductList.push(dataS[i]);
                                }
                                // 获取数据集合并传入表格中
                                table.reload('purchaseSignTaskProductTable', {
                                    data: deliveryRecordProductList,
                                    limit: deliveryRecordProductList.length
                                });

                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                  }
                    //供应商分类
                    admin.initDataSelectTree(true, 'enterprise/purchase/admin/supplierCategory/getTreeData', null, 'selectSupplierCategory', data ? data.supplierCategoryId : null, "供应商分类", "supplierCategoryId", function (data) {
                        //供应商分类ID
                        let supplierCategoryId = data.change[0].value;
                        //清空供应商分类ID
                        $('#supplierId').empty();
                        //清空采购订单列表
                        $('#purchaseOrderId').empty();
                        //供应商列表
                        admin.initDataSelect("enterprise/purchase/admin/supplierInfoPass/list", {limit: 99999, supplierCategoryId: supplierCategoryId}, "supplierId", null, "供应商", "corporateName", null);
                    });

                    //供应商列表
                    if(data){
                        admin.initDataSelect("enterprise/purchase/admin/supplierInfoPass/list", {limit: 99999, supplierCategoryId: data.supplierCategoryId}, "supplierId", data.supplierId, "供应商", "corporateName", null);
                    }

                    //供应商选择事件
                    form.on("select(supplierId)", function (selectData) {
                        //清空采购订单列表
                        $('#purchaseOrderId').empty();
                        //采购订单列表
                        admin.initDataSelect("enterprise/purchase/admin/purchaseOrder/list",{limit: 99999, filterDeliveryComplete: 1, status: "success", querySupplierId: selectData.value}, "purchaseOrderId", null, "采购订单", "sn", null);
                    });

                    //采购订单列表
                    if(data){
                        admin.initDataSelect("enterprise/purchase/admin/purchaseOrder/list",{limit: 99999, filterDeliveryComplete: 1, status: "success", querySupplierId: data.supplierId}, "purchaseOrderId", data.purchaseOrderId, "采购订单", "sn", null);

                        admin.req('enterprise/purchase/admin/purchaseOrder/list', {limit: 99999, filterDeliveryComplete: 1, status: "success",querySupplierId: data.supplierId}, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                res.data.forEach(purchaseOrder=>{
                                    if(purchaseOrder.id == data.purchaseOrderId){
                                        purchaseOrder.purchaseOrderId = purchaseOrder.id;
                                        form.val("purchaseSignTaskForm", purchaseOrder);
                                    }
                                })
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    }

                    //采购订单选择事件
                    form.on("select(purchaseOrderId)", function (selectData) {
                        admin.req('enterprise/purchase/admin/purchaseOrder/list', {limit: 99999, filterDeliveryComplete: 1, status: "success"}, function (res) {
                            layer.closeAll('loading');
                            if (res.code == 200) {
                                res.data.forEach(purchaseOrder=>{
                                    if(purchaseOrder.id == selectData.value){
                                        form.val("purchaseSignTaskForm", purchaseOrder);
                                    }
                                })
                            } else {
                                layer.msg(res.msg, {icon: 2});
                            }
                        }, 'GET');
                    });

                    //运输方式
                    admin.getDictList("transportMode", "transport_mode", data ? data.transportMode : null, "运输方式");

                    if(data){
                        if(data.transportMode == 0){
                            $("#plateNoDiv").show();
                            $("#courierNoDiv").hide();
                        }else{
                            $("#plateNoDiv").hide();
                            $("#courierNoDiv").show();
                        }
                    }

                    //运输方式选择事件
                    form.on("select(transportMode)", function (selectData) {
                        if(selectData.value == 0){
                            $("#plateNoDiv").show();
                            $("#courierNoDiv").hide();
                        }else if(selectData.value == 1){
                            $("#plateNoDiv").hide();
                            $("#courierNoDiv").show();
                        }
                    });


                    //发货数量验证
                    form.verify({
                        quantity: [/^[0-9]*(\.[0-9]{1,2})?$/, '格式错误'],
                        compare: function (data, item) {
                            var quantity = Number($(item).parent().parent().parent().prev().find("div").text());
                            if (quantity < Number(data)) {
                                return '发货数量不能大于剩余发货数！';
                            }
                        }
                    });

                    var codeArr = [];
                    var deliveryRecordProductList = [];

                    // 添加发货物料
                    $("#addProduct").click(function () {
                        var purchaseOrderId = $("form select[name='purchaseOrderId']").val();
                        if(purchaseOrderId){
                            var purchaseSignTaskProductList = admin.open({
                                type: 1,
                                area: ['80%', '85%'],
                                offset: ['10%','17%'],
                                title: '物料列表',
                                content: $('#purchaseSignTaskProductList').html(),
                                success: function () {
                                    var codes = "";
                                    for (var i = 0; i < codeArr.length; i++) {
                                        if(i == 0){
                                            codes = '"'+codeArr[i]+'"';
                                        }else{
                                            codes += "," + '"'+codeArr[i]+'"';
                                        }
                                    }
                                    // 渲染表格
                                    var insTb = table.render({
                                        elem: '#purchaseSignTaskProductListTable',
                                        url: config.base + 'enterprise/purchase/admin/purchaseOrderDetail/list',
                                        where: {purchaseOrderId: purchaseOrderId, filterDeliveryComplete: 1, codes: codes},
                                        page: false,
                                        cellMinWidth: 100,
                                        cols: [[
                                            {type: 'checkbox'},
                                            {field: 'id', align: 'center', title: 'ID', hide: 'true'},
                                            {field: 'skuCode', align: 'center', title: '物料编号', width: 160},
                                            {field: 'productName', align: 'center', title: '物料名称', width: 160},
                                            {field: 'specifications', align: 'center', title: '规格型号', width: 160},
                                            {field: 'quantity', align: 'center', title: '数量'},
                                            {field: 'unitName', align: 'center', title: '计量单位', width: 100},
                                            {field: 'rate', align: 'center', title: '税率(%)'},
                                            {field: 'taxPrice', align: 'center', title: '含税单价'},
                                            {field: 'price', align: 'center', title: '单价'},
                                            {field: 'amount', align: 'center', title: '金额'},
                                            {field: 'tax', align: 'center', title: '税额'},
                                            {field: 'totalAmount', align: 'center', title: '合计金额'}
                                        ]]
                                    });

                                    // 搜索
                                    form.on('submit(deliveryRecordProductFormSubSearchLog)', function (data) {
                                        insTb.reload({where: data.field,page: {curr: 1}},'data');
                                    });

                                    // 重置搜索
                                    form.on('submit(deliveryRecordProductResets)', function (data) {
                                        $(".lay-header").find('select').val("");
                                        $(".lay-header").find('input').val("");
                                        let field = data.field;
                                        for (let key in field) {
                                            field[key] = ''
                                        }
                                        insTb.reload({where: field, page: {curr: 1}}, 'data');
                                    });

                                    // 确认选择
                                    $('#confirmChoose').click(function () {
                                        var checkStatus = table.checkStatus('purchaseSignTaskProductListTable'); //test即为基础参数id对应的值
                                        var dataS = checkStatus.data;
                                        if (dataS.length == 0) {
                                            layer.msg("请选择物料", {icon: 2, time: 1000});
                                        }else{
                                            for (var i = 0; i < dataS.length; i++) {
                                                dataS[i].purchaseOrderDetailId =  dataS[i].id;
                                                deliveryRecordProductList.push(dataS[i]);
                                                codeArr.push(dataS[i].skuCode);
                                            }
                                            layer.close(purchaseSignTaskProductList);
                                            // 获取数据集合并传入表格中
                                            table.reload('purchaseSignTaskProductTable', {
                                                data: deliveryRecordProductList,
                                                limit: deliveryRecordProductList.length
                                            });
                                        }
                                    });



                                }
                            });
                        }else{
                            layer.msg('请选择采购订单!', {icon : 2, time : 1000});
                        }
                    })

                    if(data){
                        data.purchaseDeliveryRecordProductList.forEach(purchaseDeliveryRecordProduct=>{
                            purchaseDeliveryRecordProduct.hasDeliveryQuantity = purchaseDeliveryRecordProduct.hasSignQuantity;
                            purchaseDeliveryRecordProduct.deliveryQuantity = purchaseDeliveryRecordProduct.quantity;
                            purchaseDeliveryRecordProduct.quantity = purchaseDeliveryRecordProduct.purchaseQuantity;
                            codeArr.push(purchaseDeliveryRecordProduct.skuCode);
                            deliveryRecordProductList.push(purchaseDeliveryRecordProduct);
                        })
                    }

                    // 渲染表格
                    var insTb = table.render({
                        elem: '#purchaseSignTaskProductTable',
                        data: data ? data.purchaseDeliveryRecordProductList : [deliveryRecordProductList],
                        page: false,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers', title: '序号',height: 50}
                            ,{field: 'purchaseOrderDetailId', title: '采购订单明细ID', hide: 'true'}
                            ,{field: 'productPic', align: 'center',title: '物料图片', templet: function (d) {
                                    return '<img src="' + d.productPic + '" style="height: 40px;" id="platformPic"/>'
                                }}
                            , {field: 'skuCode', align: 'center',title: '物料编号'}
                            ,{field: 'specifications', align: 'center',title: '规格号'}
                            ,{field: 'productTypeName', align: 'center',title: '物料类型'}
                            ,{field: 'unitName', align: 'center',title: '计价单位'}
                            ,{field: 'quantity', align: 'center',title: '采购数量'}
                            ,{field: 'hasDeliveryQuantity', align: 'center',title: '已发货数'}
                            ,{field: 'surplusDeliveryQuantity', align: 'center',title: '剩余发货数',
                                templet: function (d) {
                                    return (d.quantity - d.hasDeliveryQuantity).toFixed(2);
                                }
                            }
                            ,{field: 'deliveryQuantity', align: 'center',title: '发货数量', templet: '#deliveryQuantity'}
                            ,{align: 'center', toolbar: '#purchaseSignTaskProductTableBar', title: '操作', width: 100}
                        ]]
                    });

                    // 工具条点击事件
                    table.on('tool(purchaseSignTaskProductTable)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'del') { // 删除
                            doDelete(obj);
                        }
                    });

                    // 删除发货物料
                    function doDelete(obj) {
                        var index = $(obj.tr).data("index");
                        layer.confirm('确定要删除吗？', {
                            offset: '65px',
                            skin: 'layui-layer-admin'
                        }, function (i) {
                            layer.close(i);
                            var skuCode = obj.data.skuCode;
                            if(codeArr.indexOf(skuCode) != -1){
                                deliveryRecordProductList.splice(index,1);
                                codeArr.splice(codeArr.indexOf(skuCode), 1);
                            }
                            table.reload('purchaseSignTaskProductTable', {
                                data: deliveryRecordProductList,
                                limit: deliveryRecordProductList.length
                            });
                        });
                    }

                    form.val('purchaseSignTaskForm', data);
                    // 表单提交事件
                    form.on('submit(purchaseSignTaskFormSubmit)', function (d) {
                        var purchaseDeliveryRecordProducts = [];
                        $("#productList").find("tbody tr").each(function (index, item) {
                            var purchaseDeliveryRecordProduct = {};
                            $(item).find("td").each(function (tdIndex, tdItem) {
                                if($(tdItem).data("field") == "purchaseOrderDetailId"){
                                    purchaseDeliveryRecordProduct.purchaseOrderDetailId = $(tdItem).find("div").text();
                                }
                                if($(tdItem).data("field") == "deliveryQuantity"){
                                    purchaseDeliveryRecordProduct.quantity = $(tdItem).find("input").val();
                                }
                                if($(tdItem).data("field") == "deliveryQuantity"){
                                    purchaseDeliveryRecordProduct.quantity = $(tdItem).find("input").val();
                                }
                            })
                            purchaseDeliveryRecordProducts.push(purchaseDeliveryRecordProduct);
                        })
                        if(purchaseDeliveryRecordProducts.length > 0){
                            layer.load(2);
                            d.field.purchaseDeliveryRecordProducts = purchaseDeliveryRecordProducts;
                            admin.req(data ? 'enterprise/purchase/admin/purchaseDeliveryRecord/update' : 'enterprise/purchase/admin/purchaseDeliveryRecord/add', JSON.stringify(d.field), function (res) {
                                layer.closeAll('loading');
                                if (res.code == 200) {
                                    layer.msg(res.msg, {icon: 1});
                                    table.reload('purchaseSignTaskTable');
                                    layer.closeAll('page');
                                } else {
                                    layer.msg(res.msg, {icon: 2});
                                }
                            }, data ? 'PUT' : 'POST', false, "application/json");
                        }else{
                            layer.msg("至少添加一条发货物料！", {icon: 2});
                        }
                        return false;
                    });











                }
            });
        }

        // 显示详情弹窗
        function showViewModel(data) {
            admin.open({
                type: 1,
                area: '80%',
                offset: ['10%','16%'],
                title: '采购签收任务详情',
                content: $('#purchaseSignTaskView').html(),
                success: function () {
                    //数据回显
                    form.val("purchaseSignTaskView",data);
                    if(data.transportMode == 0){
                        $("#plateNoDetailDiv").show();
                        $("#courierNoDetailDiv").hide();
                    }else{
                        $("#plateNoDetailDiv").hide();
                        $("#courierNoDetailDiv").show();
                    }

                    // 渲染表格
                    var insTb = table.render({
                        elem: '#purchaseSignTaskDetailTable',
                        url: config.base + 'enterprise/purchase/admin/purchaseDeliveryRecordProduct/list',
                        where: {purchaseDeliveryRecordId: data.id},
                        page: false,
                        cellMinWidth: 100,
                        cols: [[
                            {type: 'numbers', width: 80, title: '序号'}
                            ,{field: 'id', align: "center", title: '采购订单明细ID', hide: 'true'}
                            ,{field: 'productPic', align: "center", title: '物料图片', templet: function (d) {
                                    return '<img src="' + d.productPic + '" style="height: 40px;" id="platformPic"/>'
                                }
                            }
                            , {field: 'skuCode', align: "center", title: '物料编号'}
                            ,{field: 'specifications', align: "center", title: '规格号'}
                            ,{field: 'productTypeName', align: "center", title: '物料类型'}
                            ,{field: 'unitName', align: "center", title: '计价单位'}
                            ,{field: 'purchaseQuantity', align: "center", title: '采购数量'}
                            ,{field: 'quantity', align: "center", title: '发货数量'}
                        ]]
                    });
                }
            });
        }

        // 删除
        function doDelete(obj) {
            layer.confirm('确定要删除吗？', {
                offset: '265px',
                skin: 'layui-layer-admin'
            }, function (i) {
                layer.close(i);
                layer.load(2);
                admin.req('enterprise/purchase/admin/purchaseDeliveryRecord/' + obj.data.id, {}, function (res) {
                    layer.closeAll('loading');
                    if (res.code == 200) {
                        layer.msg(res.msg, {icon: 1});
                        obj.del();
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                }, 'DELETE');
            });
        }

        // 导出excel
        $('#purchaseSignTaskBtnExport').click(function () {
            var checkRows = table.checkStatus('purchaseSignTaskTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(insTb.config.id, checkRows.data, 'xls');
            }
        });

    });
</script>